package com.zkingsoft.zkingbase.widget;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;

/**
 * Created by ytf on 2017/1/16.
 */
public class RoundRectDrawable extends Drawable
{

    private RectF rectF;
    private Bitmap bmp;
    private Paint paint;
    private float radius;

    public RoundRectDrawable(Bitmap bmp, float radius)
    {
        this.bmp = bmp;
        this.radius = radius;
        BitmapShader shader = new BitmapShader(bmp, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setShader(shader);
    }

    @Override
    public void draw(Canvas canvas)
    {
        canvas.drawRoundRect(rectF, radius, radius, paint);
    }

    @Override
    public void setAlpha(int alpha)
    {
        paint.setAlpha(alpha);
    }

    @Override
    public int getIntrinsicWidth()
    {
        return bmp == null ? 0 : bmp.getWidth();
    }

    @Override
    public int getIntrinsicHeight()
    {
        return bmp == null ? 0 : bmp.getHeight();
    }

    @Override
    public void setBounds(Rect bounds)
    {
        super.setBounds(bounds);
        rectF = new RectF(bounds.left, bounds.top, bounds.right, bounds.bottom);
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom)
    {
        super.setBounds(left, top, right, bottom);
        rectF = new RectF(left, top, right, bottom);
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter)
    {
        paint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity()
    {
        return PixelFormat.TRANSLUCENT;
    }
}
